package com.maple.admin.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.maple.admin.misc.paging.dto.PagingParamsDTO;
import com.maple.admin.misc.paging.dto.PagingResultDTO;
import com.maple.admin.misc.paging.service.IPagingService;
import com.maple.admin.user.dto.UserDTO;
import com.maple.admin.user.po.UserPO;
import com.maple.admin.user.service.IAccountService;

@Controller
@RequestMapping("/index")
public class DefaultController {
	
	@Autowired
	IAccountService acctService;
	
	@Autowired
	IPagingService pagingService;
	
	/**
	 * main app page
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/starter")
	@ResponseBody
	public ModelAndView myStarter(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/starter_iframe"); // view 
		return mv;
	}
	
	/**
	 * data tables page
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/data/tables")
	@ResponseBody
	public ModelAndView myShowDataTables(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/user/view-user"); // view 
		return mv;
	}
	
	/**
	 * query paginated users 
	 * @param paging
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/data/tables/paging", method=RequestMethod.POST)
	@ResponseBody
	public PagingResultDTO myQueryPaginatedUsers(PagingParamsDTO paging, HttpServletRequest request, HttpServletResponse response){
		List<UserDTO> dataList = acctService.queryPaginatedUsers(paging);
		PagingResultDTO result = new PagingResultDTO();
		// data tables params
		result.setDraw(paging.getDraw());
		result.setRecordsFiltered(pagingService.countTableRows(UserPO.class));
		result.setRecordsTotal(pagingService.countTableRows(UserPO.class));
		result.setData(dataList);
		return result;
		
	}
	
	/**
	 * show add user page
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/user/add")
	@ResponseBody
	public ModelAndView myAddUserPage(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/user/add-user");
		return mv;
	}
	
	/**
	 * add user
	 * @param user
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/user/add/commit", method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> myAddUser(UserDTO user, HttpServletRequest request, HttpServletResponse response){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		Boolean alreadyExists = acctService.checkDuplicated(user.getUserName());
		if(alreadyExists == Boolean.TRUE){
			resultMap.put("status", "already_exists");
			return resultMap;
		} else{
			UUID createdID = acctService.createUser(user);
			resultMap.put("status", createdID);
		}
		return resultMap;
	}
	
	/**
	 * show add user page
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/server/info")
	@ResponseBody
	public ModelAndView myServerInfoPage(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mv = new ModelAndView();
		String hostName = request.getServerName();
		String address = request.getLocalAddr();
		String scheme = request.getScheme();
		mv.addObject("hostName", hostName);
		mv.addObject("address", address);
		mv.addObject("scheme", scheme);
		mv.setViewName("/server/info");
		return mv;
	}
	
	
}
